﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCDanhSachHS : UserControl
    {
        HocSinhBUO hsBUO = new HocSinhBUO();
        int id;
        public String id_hs;
        public UCDanhSachHS()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hsBUO.showallstudent();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hsBUO.searchbyname(tbName.Text);
        }

        private void tbName_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (rdTimTheoTen.Checked == true)
                {
                    if (tbName.Text.Equals(""))
                    {
                        MessageBox.Show("Chưa nhập từ khóa cần tìm");
                    }
                    else
                    {
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = hsBUO.searchbyname(tbName.Text);
                        dataGridView1.Visible = true;
                    }
                }
            }
        }

        private void rdTimTheoTen_CheckedChanged(object sender, EventArgs e)
        {
            tbName.Enabled = true;
            cbClass.Enabled = false;
            tbName.Text = "";
            cbClass.Text = "";
        }

        private void rdTimTheoLop_CheckedChanged(object sender, EventArgs e)
        {
            tbName.Enabled = false;
            cbClass.Enabled = true;
            cbClass.Items.Clear();
            List<string> list = hsBUO.getclassname();
            foreach (string lop in list) cbClass.Items.Add(lop);
            tbName.Text = "";
            cbClass.Text = "";
        }

        private void cbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            String classname = cbClass.Text;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hsBUO.searchbyclass(classname);
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);

                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // hiển thị ContextMenuStrip tại vị trí X,Y 
                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = cmStripListHS;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));
                        // lấy vị trí hàng và giá trị của mã học sinh tại hàng đó
                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_hs = dataGridView1.Rows[id].Cells[0].Value.ToString();

                    }
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                cmStripListHS.Visible = false;
            }
        }

        private void addStudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemHocSinh addStudent = new UCThemHocSinh();
            Form fm = this.ParentForm;
            ControlAccess.ShowConTrol(fm, addStudent, "grbHienThiChucNang");
        }

        private void editstudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaHS editstudent = new UCSuaHS(id_hs);
            Form fm = this.ParentForm;
            ControlAccess.ShowConTrol(fm, editstudent, "grbHienThiChucNang");
        }

        private void delstudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu học sinh", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (hsBUO.delstudent(id_hs))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = hsBUO.showallstudent();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (rdXemTatCa.Checked == true)
            {
                HocSinhReport r = new HocSinhReport();
                r.DataSource = dataGridView1.DataSource;
                r.Load();
                r.ShowPreviewDialog();
            }
            else if(rdTimTheoLop.Checked == true)
            {
                HocSinhReportTheoLop r1 = new HocSinhReportTheoLop();
                r1.DataSource = dataGridView1.DataSource;
                r1.Load(cbClass.Text);
                r1.ShowPreviewDialog();
            }
        }

        private void rdXemTatCa_CheckedChanged(object sender, EventArgs e)
        {
            tbName.Text = "";
            cbClass.Text = "";
            tbName.Enabled = false;
            cbClass.Enabled = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hsBUO.showallstudent();
        }
    }
}
